using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Text;
using QLVMBAYDomain;
namespace QLVMBAYDatacess
{
    public class VeGheDA : DataAccessBase
    {
		#region Constructors
		
		public VeGheDA() : base()
        {
			_selectSql = "Select * From VE_GHE";
			_insertSql = "Insert Into VE_GHE(MALOAIGHE,MALOAIVE,DIENGIAI) Values (@MALOAIGHE,@MALOAIVE,@DIENGIAI)";
        	_updateSql = "Update VE_GHE Set MALOAIGHE=@MALOAIGHE,MALOAIVE=@MALOAIVE,DIENGIAI=@DIENGIAI Where 1=1 And MAGHEVE=@MAGHEVE";
        	_deleteSql = "Delete From VE_GHE Where 1=1 And MAGHEVE=@MAGHEVE";
        	_primarSql = "Select * From VE_GHE Where 1=1 And MAGHEVE=@MAGHEVE";
        }
		
		protected static VeGheDA _inst = null;
		public static VeGheDA Inst
		{
			get
			{
				if(_inst == null)
					_inst = new VeGheDA();
				return _inst;
			}
		}
		
		#endregion
		
        #region Get Methods

		private Ve_Ghe GetCurrent(IDataReader reader, bool isDeep)
		{
			Ve_Ghe ret = new Ve_Ghe();
            ret.MaGheVe = (int)reader["MAGHEVE"];
            ret.DienGiai = reader["DIENGIAI"] != DBNull.Value ? (string)reader["DIENGIAI"] : "";
			
			ret.ItemLoaiGhe = new LoaiGhe();
			if(isDeep)
			{
                ret.ItemLoaiGhe = LoaiGheDA.Inst.GetByPrimaryKey(isDeep, (string)reader["MALOAIGHE"]);
			}
			else
                ret.ItemLoaiGhe.MaLoaiGhe = (string)reader["MALOAIGHE"];
			
			ret.ItemLoaiVe = new LoaiVe();
			if(isDeep)
			{
                ret.ItemLoaiVe = LoaiVeDA.Inst.GetByPrimaryKey(isDeep, (string)reader["MALOAIVE"]);
			}
			else
                ret.ItemLoaiVe.MaLoaiVe = (string)reader["MALOAIVE"];
			
			
			return ret;
		}

		public List<Ve_Ghe> GetAll(bool isDeep)
		{
			DbCommand comm = GetCommand(_selectSql);
            using (Connection)
            {
                using (DbDataReader dr = comm.ExecuteReader())
                {
                    List<Ve_Ghe> ret = new List<Ve_Ghe>();
					while(dr.Read())
					{
						ret.Add(GetCurrent(dr, isDeep));
					}
					return ret;
                }
            }
		}

        public Ve_Ghe GetByPrimaryKey(bool isDeep, params object[] args)
		{
			DbCommand cmd = GetCommand(_primarSql);
			if(args.Length < 1)
			{
				throw new Exception();
			}
            SetParam(cmd, "@MAGHEVE", args[0]);

            using (Connection)
            {
                using (DbDataReader dr = cmd.ExecuteReader())
                {
					if(dr.Read())
					{
						return GetCurrent(dr, isDeep);
					}
					return null;
                }
            }
		}

        public bool Insert(Ve_Ghe entity)
		{
			DbCommand cmd = GetCommand(_insertSql);            
            SetParam(cmd, "@DIENGIAI", entity.DienGiai);

			SetParam(cmd, "@MALOAIGHE", entity.ItemLoaiGhe.MaLoaiGhe);
			SetParam(cmd, "@MALOAIVE", entity.ItemLoaiVe.MaLoaiVe);
			
            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}

        public bool Update(Ve_Ghe entity)
		{
			DbCommand cmd = GetCommand(_updateSql);
            SetParam(cmd, "@MAGHEVE", entity.MaGheVe);
            SetParam(cmd, "@DIENGIAI", entity.DienGiai);

			SetParam(cmd, "@MALOAIGHE", entity.ItemLoaiGhe.MaLoaiGhe);
			SetParam(cmd, "@MALOAIVE", entity.ItemLoaiVe.MaLoaiVe);
			
            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}

        public bool Delete(Ve_Ghe entity)
		{
			DbCommand cmd = GetCommand(_deleteSql);
            SetParam(cmd, "@MAGHEVE", entity.MaGheVe);

            using (Connection)
            {
				int ret = cmd.ExecuteNonQuery();
				return ret > 0;
            }
		}
		
		#endregion
    }
}


